package com.cn.kehong.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IProductPeiJizhongCollectService;
import com.cn.kehong.dao.ProductPeiJizhongCollectMapper;
import com.cn.kehong.domain.ProductPeiJizhongCollect;
import com.cn.kehong.domain.ProductPeiJizhongCollectExample;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Na;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;


@EnableScheduling
@Service
public class ProductPeiJizhongCollectServiceImpl implements IProductPeiJizhongCollectService {


    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    private ProductPeiJizhongCollectMapper productPeiJizhongCollectMapper;

    @Override
    public List<ProductPeiJizhongCollect> getProductPeiJizhongCollects(String factoryName, String guannum) throws Exception {
        ProductPeiJizhongCollectExample example = new ProductPeiJizhongCollectExample();
        example.createCriteria().andFactoryNameEqualTo(factoryName).andGuannumEqualTo(guannum);
        example.setOrderByClause("create_time desc");
        return productPeiJizhongCollectMapper.selectByExample(example);
    }


    @Override
    public String updateProductPeiJizhongCollectById(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "";
        }
        ProductPeiJizhongCollectExample example = new ProductPeiJizhongCollectExample();
        example.createCriteria().andIdEqualTo(id);
        ProductPeiJizhongCollect update = new ProductPeiJizhongCollect();
        update.setUsed("1");
        int count = productPeiJizhongCollectMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public ProductPeiJizhongCollect getJiZhongData(String factoryName,String codeNum) throws Exception {
        List<ProductPeiJizhongCollect> collects = getProductPeiJizhongCollects(factoryName,codeNum);
        if(CollectionsUtil.isEmpty(collects)){
            return new ProductPeiJizhongCollect();
        }
        return collects.get(0);
    }

    private void getJiZhongRedisData(String key,List<String> allRedisList){
        long roomSize = redisTemplate.opsForList().size(key);
        for (int i = 0; i < roomSize; i++) {
            Object value = redisTemplate.opsForList().rightPop(key);
            if (null != value) {
                allRedisList.add(value.toString());
            }
        }
    }

    @Scheduled(cron = "*/5 * * * * ?")
    public void actionJiZhongData(){
        List<String> allRedisList = Lists.newArrayList();
        getJiZhongRedisData("162tl1",allRedisList);
        getJiZhongRedisData("162tl2",allRedisList);
        getJiZhongRedisData("161tl1",allRedisList);
        getJiZhongRedisData("161tl2",allRedisList);

        List<ProductPeiJizhongCollect> jizhongCollects = Lists.newLinkedList();
        for (int i = 0; i < allRedisList.size(); ++i) {
            try {
                JSONObject roomObject = JSON.parseObject(allRedisList.get(i));
                String Name = roomObject.getString("Name");
                String factoryName = StringUtils.equals("161tl1", Name)?"16-1":"16-2";
                JSONArray array = roomObject.getJSONArray("guanzilist");
                for(int j =0; j < array.size();j++){
                    JSONObject jsonObject = array.getJSONObject(j);
                    String guannum = jsonObject.getString("guannum");
                    String guantouliao = jsonObject.getString("guantouliao");
                    String tstarttime = jsonObject.getString("tstarttime");
                    String tendtime = jsonObject.getString("tstarttime");
                    String guanli = jsonObject.getString("guanli");
                    String guantie = jsonObject.getString("guantie");
                    ProductPeiJizhongCollect collect = new ProductPeiJizhongCollect();
                    collect.setFactoryName(factoryName);
                    collect.setGuannum(guannum);
                    collect.setGuantouliao(guantouliao);
                    collect.setTstarttime(tstarttime);
                    collect.setTendtime(tendtime);
                    collect.setGuanli(guanli);
                    collect.setGuantie(guantie);
                    collect.setId(CommonUtil.generateRandomNum("jzCollect-"));
                    jizhongCollects.add(collect);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(CollectionsUtil.isNotEmpty(jizhongCollects)){
            productPeiJizhongCollectMapper.batchInsertPeiJiZhongCollects(jizhongCollects);
        }
    }
}
